package com.example.project.service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.project.entity.IssueCategorie;
import com.example.project.entity.IssuePrioritie;
import com.example.project.entity.IssueStatus;
import com.example.project.entity.Project;
import com.example.project.entity.TimeActivitie;
import com.example.project.entity.Tracker;
import com.example.project.entity.User;
import com.example.project.mapper.IssueCategorieMapper;
import com.example.project.mapper.IssuePrioritieMapper;
import com.example.project.mapper.IssueStatusMapper;
import com.example.project.mapper.ProjectMapper;
import com.example.project.mapper.TimeActivitieMapper;
import com.example.project.mapper.TrackerMapper;
import com.example.project.mapper.UserMapper;
import com.example.project.utils.CacheUtils;

/**
 * 通用服务类，提供项目中常用的数据缓存和映射功能
 */
@Service
public class CommonService {

	@Autowired
	private ProjectMapper projectMapper;

	@Autowired
	private IssueStatusMapper issueStatusMapper;

	@Autowired
	private TrackerMapper trackerMapper;

	@Autowired
	private IssuePrioritieMapper issuePrioritieMapper;

	@Autowired
	private IssueCategorieMapper categorieMapper;

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private TimeActivitieMapper timeActivitieMapper;

	public User getAdministrator() {
		// TODO Auto-generated method stub
		User administrator = userMapper.getAdministrator();
		return administrator;
	}

	/**
	 * 获取项目ID到项目对象的映射
	 *
	 * @return 项目ID到项目对象的映射
	 */
	public Map<Long, Project> getProjectMap() {
		// 项目名称
		List<Project> projectList = getProjectList();
		Map<Long, Project> projectMap = projectList.stream().collect(Collectors.toMap(Project::getId, info -> info));
		return projectMap;
	}

	/**
	 * 获取项目列表，优先从缓存中读取
	 *
	 * @return 项目列表
	 */
	public List<Project> getProjectList() {
		@SuppressWarnings("unchecked")
		List<Project> projectList = (List<Project>) CacheUtils.get("projectList");
		if (projectList == null) {
			projectList = projectMapper.selectProjectAll(1);
			CacheUtils.put("projectList", projectList);
		}
		return projectList;
	}

	public Project getProjectInfo(Long projectId) {
		// TODO Auto-generated method stub
		if (projectId != null) {
			Map<Long, Project> projectMap = this.getProjectMap();
			return projectMap.get(projectId);
		}
		return null;
	}

	public Map<Long, IssueCategorie> getIssueCategorieMap() {
		List<IssueCategorie> issueStatusList = getIssueCategorieList();
		// 状态名称
		Map<Long, IssueCategorie> categorieMap = issueStatusList.stream()
				.collect(Collectors.toMap(IssueCategorie::getId, info -> info));
		return categorieMap;
	}

	/**
	 * 获取分类列表
	 *
	 * @return 分类列表
	 */
	public List<IssueCategorie> getIssueCategorieList() {
		@SuppressWarnings("unchecked")
		List<IssueCategorie> categorieList = (List<IssueCategorie>) CacheUtils.get("categorieList");
		if (categorieList == null) {
			categorieList = categorieMapper.selectIssueCategoriesAll();
			CacheUtils.put("categorieList", categorieList);
		}
		return categorieList;
	}

	/**
	 * 获取问题状态ID到状态对象的映射
	 *
	 * @return 问题状态ID到状态对象的映射
	 */
	public Map<Long, IssueStatus> getIssueStatusMap() {
		List<IssueStatus> issueStatusList = getIssueStatusList();
		// 状态名称
		Map<Long, IssueStatus> statusMap = issueStatusList.stream()
				.collect(Collectors.toMap(IssueStatus::getId, info -> info));
		return statusMap;
	}

	/**
	 * 获取问题状态列表，优先从缓存中读取
	 *
	 * @return 问题状态列表
	 */
	public List<IssueStatus> getIssueStatusList() {
		@SuppressWarnings("unchecked")
		List<IssueStatus> issueStatusList = (List<IssueStatus>) CacheUtils.get("issueStatusList");
		if (issueStatusList == null) {
			issueStatusList = issueStatusMapper.selectIssueStatusAll();
			CacheUtils.put("issueStatusList", issueStatusList);
		}
		return issueStatusList;
	}

	/**
	 * 获取跟踪类型ID到跟踪类型对象的映射
	 *
	 * @return 跟踪类型ID到跟踪类型对象的映射
	 */
	public Map<Long, Tracker> getTrackerMap() {
		// 跟踪类型名称
		List<Tracker> trackerList = getTrackerList();
		Map<Long, Tracker> trackerMap = trackerList.stream().collect(Collectors.toMap(Tracker::getId, info -> info));
		return trackerMap;
	}

	/**
	 * 获取跟踪类型列表，优先从缓存中读取
	 *
	 * @return 跟踪类型列表
	 */
	private List<Tracker> getTrackerList() {
		@SuppressWarnings("unchecked")
		List<Tracker> trackerList = (List<Tracker>) CacheUtils.get("trackerList");
		if (trackerList == null) {
			trackerList = trackerMapper.selectTrackersAll();
			CacheUtils.put("trackerList", trackerList);
		}
		return trackerList;
	}

	/**
	 * 获取问题优先级ID到优先级对象的映射
	 *
	 * @return 问题优先级ID到优先级对象的映射
	 */
	public Map<Long, IssuePrioritie> getIssuePrioritieMap() {
		// 优先级
		List<IssuePrioritie> issuePrioritieList = getIssuePrioritieList();
		Map<Long, IssuePrioritie> issuePrioritieMap = issuePrioritieList.stream()
				.collect(Collectors.toMap(IssuePrioritie::getId, info -> info));
		return issuePrioritieMap;
	}

	/**
	 * 获取问题优先级列表，优先从缓存中读取
	 *
	 * @return 问题优先级列表
	 */
	public List<IssuePrioritie> getIssuePrioritieList() {
		@SuppressWarnings("unchecked")
		List<IssuePrioritie> issuePrioritieList = (List<IssuePrioritie>) CacheUtils.get("issuePrioritieList");
		if (issuePrioritieList == null) {
			issuePrioritieList = issuePrioritieMapper.findAllList();
			CacheUtils.put("issuePrioritieList", issuePrioritieList);
		}
		return issuePrioritieList;
	}

	/**
	 * 获取用户ID到用户对象的映射
	 *
	 * @return 用户ID到用户对象的映射
	 */
	public Map<Long, User> getUserMap() {
		List<User> userList = getUserList();
		Map<Long, User> userMap = userList.stream().collect(Collectors.toMap(User::getUserId, info -> info));
		return userMap;
	}

	public Map<String, User> getUserMapByLoginName() {
		List<User> userList = getUserList();
		Map<String, User> userMap = userList.stream().collect(Collectors.toMap(User::getLoginName, info -> info));
		return userMap;
	}

	/**
	 * 获取用户列表，优先从缓存中读取
	 *
	 * @return 用户列表
	 */
	public List<User> getUserList() {
		@SuppressWarnings("unchecked")
		List<User> userList = (List<User>) CacheUtils.get("userList");
		if (userList == null) {
			userList = userMapper.selectUserAll();
			CacheUtils.put("userList", userList);
		}
		return userList;
	}

	public User getUserInfo(long userId) {
		// TODO Auto-generated method stub
		Map<Long, User> userMap = this.getUserMap();
		User user = userMap.get(userId);
		if (user == null) {
			user = userMapper.selectUserById(userId);
		}
		return user;
	}

	/**
	 * 时间跟踪
	 * 
	 * @return
	 */
	public List<TimeActivitie> getTimeActivitieList() {
		@SuppressWarnings("unchecked")
		List<TimeActivitie> timeActivitieList = (List<TimeActivitie>) CacheUtils.get("timeActivitieList");
		if (timeActivitieList == null) {
			timeActivitieList = timeActivitieMapper.selectTimeActivitieList(null);
			CacheUtils.put("timeActivitieList", timeActivitieList);
		}
		return timeActivitieList;
	}

	public Map<Long, TimeActivitie> getTimeActivitieMap() {
		List<TimeActivitie> timeActivitieList = getTimeActivitieList();
		Map<Long, TimeActivitie> timeActivitieMap = timeActivitieList.stream()
				.collect(Collectors.toMap(TimeActivitie::getId, info -> info));
		return timeActivitieMap;
	}

}
